//
//  SteamListManager.swift
//  MyVideo
//
//  Created by han on 2021/4/22.
//

import Foundation

class StreamListManager {
    static let shared: StreamListManager = StreamListManager()
    
    var streams: [Stream]!
    
    private var streamMap = [String: Stream]()
    
    private init() {
        do {
            guard let streamsFilepath = Bundle.main.url(forResource: "Streams", withExtension: "plist") else { return }
            let data = try Data(contentsOf: streamsFilepath)
            let decoder = PropertyListDecoder()
            streams = try decoder.decode([Stream].self, from: data)
            streams.forEach { streamMap[$0.name] = $0 }
        } catch  {
            fatalError("An error occured when reading the Streams.plist file: \(error.localizedDescription)")
        }
    }
    
    func stream(with name: String) -> Stream {
        guard let stream = streamMap[name] else { fatalError("Could not find `Stream` with name: \(name)") }
        return stream
    }
    
}
